from PyQt4 import QtCore
import sys,os,inspect
sys.path.insert(0,os.path.abspath(os.path.join(os.getcwd(), os.pardir)))## add root path 
from KDataBase.KDBManager import KDBManager
from KDataBase import KBuildDB,KQueryDB,KDBUtility
from KDataBase.KUpdateDB import KUpdateDB
from KModel.KSingleStock import KSingleStock
class KTradeConsole(QtCore.QObject):

    def __init__(self):
        super(KTradeConsole, self).__init__()

    def RunTest(self):
        print(KDBUtility.KDBUtility.GetDailyUpdateFilePath())
        ##self.manger=KDBManager()
        ##self.manger.ImportHistorical("D:\\Dropbox\\Trading2013\\Data\\EOD","EOD")
        self.db=KBuildDB.KBuildDB()
        ##self.db.ThreadArgs=["D:\\Dropbox\\Trading2013\\Data\\Daily\\stooq_all","Stooq"]
        #self.db.BuildAllHistory("D:\\Dropbox\\Trading2013\\Data\\Daily\\EOD","EOD")
        #self.db.BuildAllHistory("D:\\Dropbox\\Trading2013\\Data\\Daily\\stooq_us","Stooq")
        #self.db.ConsolidateToSymbolBased()
        self.db.ThreadArgs={"Op":"OneClickImport","Path":"D:\\Dropbox\\Trading2013\\Data\\Daily"}
        self.db.DoWork()  
        ##self.db.ConsolidateToSymbolBased()
        #self.db=KUpdateDB()
        
        #self.db.ThreadArgs={"Op":"OneClickImport","Path":"D:/Dropbox/Trading2013/Data/ToUpdate/5_us_txt.zip","Provider":"Stooq"}
        #self.db.ProcessUpdateFile("D:/Dropbox/Trading2013/Data/ToUpdate/5_us_txt.zip","Stooq",True)
        ##self.provider.UpdateHistory(r"D:/Dropbox/Trading2013/Data/ToUpdate/temp/data/5 min","5min")
        #self.stk=KSingleStock()
        #self.stk.Initialize('us','nasdaq','aapl',['20100101','20110101'])
        #self.kdb.QueryAllStocksByMkt("nasdaq")
        #print(self.kdb.QueryStockByDateRange("nasdaq","aapl","",""))
        #results=KQueryDB.KQueryDB.QueryAllStockNames()
        results=KQueryDB.KQueryDB.QueryStockByDateRange('us', 'nasdaq','aapl')

        print(len(results))
if __name__ == "__main__":
    app = QtCore.QCoreApplication(sys.argv)
    console = KTradeConsole()
    useProfile=False
    if useProfile:
        import cProfile
        cProfile.run("console.RunTest()", "prof.txt")
        import pstats
        p = pstats.Stats("prof.txt")
        p.sort_stats("cumulative").print_stats()  
    else:
        console.RunTest()
    
    #console.RunTest()
    sys.exit(app.exec_())